<template>
  <el-form style="width: 60%" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="书名" prop="name">
      <el-input v-model="ruleForm.name" placeholder="请输入书名"></el-input>
    </el-form-item>
    <el-form-item label="作者" prop="author">
      <el-input v-model="ruleForm.author" placeholder="请输入作者"></el-input>
    </el-form-item>
    <el-form-item label="发布者" prop="publish">
      <el-input v-model="ruleForm.publish" placeholder="请输入发布者"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
name: "AddBook",
  data() {
    return {
      ruleForm: {
        name: '',
        author: '',
        publish: '',
        // delivery: false,
      },
      rules: {
        name: [
          { required: true, message: '图书名称不能为空', trigger: 'blur' },
        ],
        author: [
          { required: true, message: '图书作者不能为空', trigger: 'blur' },
        ],
        publish: [
          { required: false, message: '图书发布者不能为空', trigger: 'blur' },
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('http://localhost:8181/book/save',this.ruleForm).then(function (resp){
            if(resp.data == "success"){
              _this.$alert('《'+_this.ruleForm.name+'》添加成功！',"消息",
                  { confirmButtonText: '确定',callback:action => {
                      _this.$router.push("/selectBook")
                }});
            }
          })
        }else{
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>